<?php
namespace StudyBuddy\Controllers;

use \StudyBuddy\WebPage;
use \StudyBuddy\Acl\Acl;
use \StudyBuddy\Request;
use \StudyBuddy\Responder;

/**
 * Controller for changing user group (A.K.A. role)
 * of user
 * 
 * This controller processes the POST
 * request from the drop-down user groups menu
 * on the Userinfo page
 *
 */
class Usergroup extends WebPage{


	protected $permission = 'change_user_role';

	protected $bRequirePost = true;

	protected $requireToken = true;

	protected $aRequired = array('uid', 'role');

	protected function main(){
		$this->setRole()
		->setReturn();
	}

	protected function setRole(){
		$uid = $this->oRequest['uid'];
		$role = $this->oRequest['role'];
		d('uid: '.$uid.' role: '.$role);

		$update = array('$set' => array('role' => $role));
		$res = $this->oRegistry->Mongo->USERS->update(array('_id' => $uid), $update);
		d('$res: ' .$res);

		return $this;
	}


	protected function setReturn(){
		if(Request::isAjax()){
			$ret = array(
			'alert' => 'User group updated',
			'reload' => 1000);
				
			Responder::sendJSON($ret);
		}

		Responder::redirectToPage();
	}
}
